package com.wlh.animation.demo.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 *
 * Created by weilh on 2018/3/5.
 */

public class CellingScrollChildView extends LinearLayout implements NestedScrollingChild{
    NestedScrollingChildHelper childHelper;
    public CellingScrollChildView(Context context) {
        super(context);
        init();
    }

    public CellingScrollChildView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CellingScrollChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CellingScrollChildView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        childHelper = new NestedScrollingChildHelper(this);
        childHelper.setNestedScrollingEnabled(isNestedScrollingEnabled());
    }

    float lastY;
    int[] consumed = new int[2];
    int[] offset = new int[2];
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN :
                startNestedScroll(getNestedScrollAxes());
                lastY = event.getY();
                break;
            case MotionEvent.ACTION_UP :
                stopNestedScroll();
                break;
            case MotionEvent.ACTION_MOVE:
                int dyConsumed = (int)(lastY - event.getY());
                dispatchNestedPreScroll(0, dyConsumed, consumed, offset);
                int y = dyConsumed - consumed[1];
                if (getScrollY() + y < 0) {
                    y = 0;
                }
                scrollBy(0, y);
                break;

        }
        return true;
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return childHelper.startNestedScroll(axes);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return true;
    }

    @Override
    public void stopNestedScroll() {
        childHelper.stopNestedScroll();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,
                                        @Nullable int[] offsetInWindow) {
        return childHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow) {
        return childHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public int getNestedScrollAxes() {
        return ViewCompat.SCROLL_AXIS_VERTICAL;
    }
}
